package org.yu.lemon.platform.common.domain;

import lombok.Getter;
import lombok.Setter;
import org.springframework.lang.NonNull;

import java.time.Instant;

@Getter
@Setter
public class Result<T> {

    /**
     * 结果状态 ,具体状态码参见 {@link ResultCodeEnum}
     */
    private int code;
    private String message;
    private T data;
    private long timestamp ;

    public Result() {
        this.timestamp = Instant.now().toEpochMilli();
    }

    public Result(@NonNull ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.timestamp = Instant.now().toEpochMilli();
    }

    public Result(T data) {
        this.code = ResultCodeEnum.SUCCESS.getCode();
        this.message = ResultCodeEnum.SUCCESS.getMessage();
        this.data = data;
        this.timestamp = Instant.now().toEpochMilli();
    }

    public Result(int code, String message) {
        this.code = code;
        this.message = message;
        this.timestamp = Instant.now().toEpochMilli();
    }


    public static @NonNull <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static @NonNull <T> Result<T> fail(ResultCode resultCode) {
        return new Result<>(resultCode);
    }

    public static @NonNull <T> Result<T> fail(int code, String message) {
        return new Result<>(code, message);
    }
}
